package com.rfid.emergencywarehouse.presenter;

import com.rfid.emergencywarehouse.net.entity.WGStatusRes;
import com.rfid.emergencywarehouse.view.IAddAssetView;
import com.rfid.emergencywarehouse.entity.NameValueBean;
import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.net.entity.CreateStocklRes;
import com.rfid.emergencywarehouse.net.entity.GetMaterialCategoryListRes;
import com.rfid.emergencywarehouse.net.entity.MaterialModeRes;
import com.rfid.emergencywarehouse.net.entity.MaterilListRes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AddAssetPresenter extends ReadTagPresenter<IAddAssetView> {

    public AddAssetPresenter(){
        super();
        setDisableShelf(true);
    }

    public void getMaterialModeList(String materialId) {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    MaterialModeRes res = NetApi.INSTANCE.getMaterialModeList(materialId);
                    List<NameValueBean> beans = new ArrayList<>();
                    for (MaterialModeRes.DataBean bean : res.getData()) {
                        NameValueBean bean1 = new NameValueBean();
                        bean1.setName(bean.getDisplayName());
                        bean1.setValue(bean.getId() + "");
                        beans.add(bean1);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onGetMateralModeList(beans);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getMaterialList(String categoryId) {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    MaterilListRes res = NetApi.INSTANCE.getMaterialList(categoryId);
                    List<NameValueBean> beans = new ArrayList<>();
                    for (MaterilListRes.DataBean bean : res.getData()) {
                        NameValueBean bean1 = new NameValueBean();
                        bean1.setName(bean.getDisplayName());
                        bean1.setValue(bean.getId() + "");
                        beans.add(bean1);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onGetMaterialList(beans);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getMaterialCategoryList() {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    GetMaterialCategoryListRes res = NetApi.INSTANCE.getMaterialCategoryListRes();
                    List<NameValueBean> beans = new ArrayList<>();
                    for (GetMaterialCategoryListRes.DataBean bean : res.getData()) {
                        NameValueBean bean1 = new NameValueBean();
                        bean1.setName(bean.getDisplayName());
                        bean1.setValue(bean.getId() + "");
                        beans.add(bean1);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onGetMaterialCategoryList(beans);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void createStock(List<String> barcodes,String materialId, String skuId,String shapeStatus) {
        mView.onShowLoading("正在新增物资");
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                boolean success = false;
                String msg = "";
                try {
                    CreateStocklRes res = NetApi.INSTANCE.addStock(barcodes,materialId,skuId,shapeStatus);
                    if (res.getCode() == 0) {
                        success = true;
                    } else {
                        msg = res.getMsg();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    msg = e.getMessage();
                    success = false;
                }
                String finalMsg = msg;
                boolean finalSuccess = success;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mView != null) {
                            mView.onHideLoading();
                            mView.onCreateStoclResult(barcodes,finalSuccess, finalMsg);
                        }
                    }
                });
            }
        });
    }

    public void getWGStatusConfigs() {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    WGStatusRes res = NetApi.INSTANCE.getWGStatusConfig();
                    String value = res.getData().getValue();
                    String values[] = value.split(",");
                    List<NameValueBean> beans = new ArrayList<>();
                    for (String v : values) {
                        NameValueBean b = new NameValueBean();
                        b.setName(v);
                        b.setValue(v);
                        beans.add(b);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onGetWGStatusConfigs(beans);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected boolean onRfid(String epc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mView!= null) {
                    mView.onBarcode(epc,epc);
                }
            }
        });
        return false;
    }
}
